package com.meichis.yndms.adapter.RecycleViewItemType;

import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.yndms.R;
import com.meichis.yndms.model.EventBusRadioButton;
import com.meichis.yndms.model.InspectItemOption;
import com.meichis.yndms.model.InspectTemplate_Item;

import java.util.List;

public class FillMode_RadioButtonNew implements ItemViewDelegate<InspectTemplate_Item> {
    private List<InspectTemplate_Item> items;

    public FillMode_RadioButtonNew(List<InspectTemplate_Item> items) {
        this.items = items;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_rg_new;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 3;
    }

    @Override
    public void convert(ViewHolder holder, final InspectTemplate_Item item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        View v_bottom = holder.getView(R.id.v_bottom);

        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
            }
        }

        if ((position + 1) == items.size()) {//如果是最后一个，则显示底部圆角边框
            v_bottom.setVisibility(View.VISIBLE);
        } else {
            //如果当前位置的itemGroup 与下一个位置的itemGroup 不同 ：不是同一个分类
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position + 1).getItemGroup())) {
                v_bottom.setVisibility(View.VISIBLE);
            } else {
                v_bottom.setVisibility(View.GONE);
            }
        }

        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        final RadioGroup rg = holder.getView(R.id.rg_answer);
        rg.removeAllViews();
        int count = item.getItem().getOptions().size();
        String affectProcess = "";
        for (InspectItemOption option : item.getItem().getOptions()) {
            if (!TextUtils.isEmpty(option.getRemark())) {
                affectProcess = option.getRemark();
            }
        }
        for (int i = 0; i < count; i++) {
            RadioButton radioButton = new RadioButton(holder.getConvertView().getContext());
            radioButton.setButtonDrawable(holder.getConvertView().getContext().getResources().getDrawable(R.drawable.radiobutton));
            radioButton.setPadding(30, 20, 20, 20);
            radioButton.setTextColor(Color.BLACK);
            radioButton.setTextSize(13);
            radioButton.setId(Tools.generateViewId());
            final String finalAffectProcess = affectProcess;
            final int finalI = i;
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    item.setFillValue((String) v.getTag());
                    for (InspectItemOption option : item.getItem().getOptions()) {
                        if (option.getCode().equals(item.getFillValue())) {
                            item.setFillName(option.getName());
                        }
                    }
                    if (!TextUtils.isEmpty(finalAffectProcess)) {
                        if (!TextUtils.isEmpty(item.getItem().getOptions().get(finalI).getRemark())) {
                            EventBusRadioButton e = new EventBusRadioButton();
                            e.setFillMode(2);
                            e.setInspectTemplateName(finalAffectProcess);
                            //EventBus.getDefault().post(e);
                        } else {
                            EventBusRadioButton e = new EventBusRadioButton();
                            e.setFillMode(22);
                            e.setInspectTemplateName(finalAffectProcess);
                            //EventBus.getDefault().post(e);
                        }
                    }
                }
            });

            radioButton.setText(Html.fromHtml(item.getItem().getOptions().get(i).getName() + "\t\t <font  color=\"#8D8D8D\"><strong>" + item.getItem().getOptions().get(i).getRemark() + "</strong></font>"));
            radioButton.setTag(item.getItem().getOptions().get(i).getCode());
            //等于这个Value 设置Checked
            radioButton.setChecked(item.getFillValue().equals(item.getItem().getOptions().get(i).getCode()));
            rg.addView(radioButton, RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    private void setBackground(InspectTemplate_Item item, RadioGroup ll) {
        ll.setBackgroundResource(item.getFillName().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame_red : R.drawable.bg_ll_frame);
    }
}
